<?php
class Sfs_Controller_Action_Helper_DownloadFile  extends  Zend_Controller_Action_Helper_Abstract
{
    public function direct($filename, $path='')
    {
        $front = Zend_Controller_Front::getInstance();
        Zend_Controller_Action_HelperBroker::getStaticHelper('layout')->disableLayout();
        Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer')->setNorender();

        if ($path == '')
        $data_path = Bootstrap::getPath('data').'/'.$front->getRequest()->getModuleName();
        else
        $data_path =  Bootstrap::getPath('data').'/'.$path;

        $filename = $data_path.'/'.$filename;

        $filename = realpath($filename);
//echo strrchr($filename,".");
        $file_extension = strtolower(substr(strrchr($filename,"."),1));

        switch ($file_extension) {
            case "pdf": $ctype="application/pdf"; break;
            case "exe": $ctype="application/octet-stream"; break;
            case "zip": $ctype="application/zip"; break;
            case "odt": $ctype="application/vnd.oasis.opendocument.text";break;
            case "doc": $ctype="application/msword"; break;
            case "ods": $ctype="application/vnd.oasis.opendocument.spreadsheet";break;
            case "xls": $ctype="application/vnd.ms-excel"; break;
            case "odp": $ctype="application/vnd.oasis.opendocument.presentation";break;
            case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
            case "gif": $ctype="image/gif"; break;
            case "png": $ctype="image/png"; break;
            case "mp3": $ctype="audio/mpeg3";break;
            case "wav": $ctype="audio/wav";break;
            case "mpeg":
            case "mpg": $ctype="video/mpeg";break;
            case "avi": $ctype="video/avi";break;
            case "jpeg":
            case "jpg": $ctype="image/jpg"; break;
            default: $ctype="application/force-download";
        }

        if (!file_exists($filename)) {

            die("NO FILE HERE");
        }

        $front->getResponse()->setHeader("Pragma","public");
        $front->getResponse()->setHeader("Expires", 0);
        $front->getResponse()->setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
        $front->getResponse()->setHeader("Cache-Control: ","private",false);
        $front->getResponse()->setHeader("Content-Type",$ctype);
        $front->getResponse()->setHeader("Content-Disposition","attachment; filename=\"".basename($filename)."\";");
        $front->getResponse()->setHeader("Content-Transfer-Encoding","binary");
        $front->getResponse()->setHeader("Content-Length",@filesize($filename));
        set_time_limit(0);
        @readfile("$filename") or die("File not found.");
    }
}